/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package smaragJava;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author northpole
 */
class UserDAO implements UserDAOInterface {

	Connector con;
	
	public UserDAO(Connector con){
		this.con = con;
	}
	
	public void insert(User user) {
		String query;
		Statement s;
		String [] values = {user.getEmail(), user.getPassword(), user.getFname(), user.getLname(), user.getEducation(), user.getOccupation()};
		
		query = con.abstractInsert("users", values);
		try{
			s = con.getConnection().createStatement();
			s.executeUpdate(query);
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}

	// throws CustomerDAOException;

	public void update(User user) {
		String query;
		Statement s;
		String [] values = {user.getEmail(), user.getPassword(), user.getFname(), user.getLname(), user.getEducation(), user.getOccupation()};
		
		query = con.abstractUpdate(values, user.getId(), "users");
		try{
			s = con.getConnection().createStatement();
			s.executeUpdate(query);
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}

	// throws CustomerDAOException;

	public void delete(String username/* , User ,id */) {
		String[] fields = { "*" };
		String[] usernames = { username };
		String query;
		Statement s;
		
		query = con.abstractDelete(fields, usernames, "username", "users");
		try{
			s = con.getConnection().createStatement();
			s.execute(query);
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		
		throw new UnsupportedOperationException("Not yet implemented");
	}

	// throws CustomerDAOException;

	public Vector<User> findAll() {

            try {
            String[] fields = {"*"};
            String query;
            Statement s;
            ResultSet rs;
            Vector<User> vector = new Vector();
            query = con.abstractSelect("users");
            PreparedStatement stmt = con.getConnection().prepareStatement(query);
            rs = stmt.executeQuery();
            try {
                s = con.getConnection().createStatement();
                rs = s.executeQuery(query);
                while (rs.next()) {
                    User user = new User();
                    user.setEmail(rs.getString("email"));
                    user.setFname(rs.getString("fname"));
                    user.setLname(rs.getString("lname"));
                    user.setUsername(rs.getString("username"));
                    vector.add(user);
                    user = null;
                }
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return vector;
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
	}

	// throws CustomerDAOException;

	public User findByPrimaryKey(String id) {
		User user = null;
		Statement s;
		ResultSet rs;
		String query;
		
		String[] fields = { "*" };
		String[] ids = { id };

		query = con.abstractSelect(fields, ids, "id", "users");
		try{
			s = con.getConnection().createStatement();
			rs = s.executeQuery(query);
					
			user = new User();
			
			user.setEmail(rs.getString("email"));
			user.setFname(rs.getString("fname"));
			user.setLname(rs.getString("lname"));
			user.setUsername(rs.getString("username"));						
			
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		// con.executeQuer();
		return user;
	}

	// throws CustomerDAOException;

	public User findByUsername(String username) {
		
		User user = null;
		Statement s;
		ResultSet rs;
		String query;
		
		String[] fields = { "*" };
		String[] usernames = { username };
		
		query = con.abstractSelect(fields, usernames, "username", "users");
		try{
			s = con.getConnection().createStatement();
			rs = s.executeQuery(query);
					
			user = new User();
			
			user.setEmail(rs.getString("email"));
			user.setFname(rs.getString("fname"));
			user.setLname(rs.getString("lname"));
			user.setUsername(rs.getString("username"));						
			
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		// con.executeQuer();
		return user;
	}

}
